package com.training.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.base.BaseAdapter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.training.R;
import com.training.bean.Course;
import com.training.inter.OnCourseListItemClickListener;
import com.training.utils.DateUtil;
import com.training.utils.DensityUtils;
import com.training.widget.CircularProgressView;


public class CourseListAdapter extends BaseAdapter<Course, CourseListAdapter.ViewHolder> {
    private OnCourseListItemClickListener listener;

    public CourseListAdapter(Context context, OnCourseListItemClickListener listener) {
        super(context);
        this.listener = listener;
    }

    @Override
    protected void onBindView(ViewHolder holder, int position, Course course) {
        holder.title.setText(course.title);
        // 加载圆角图片
        Glide.with(mContext)
                .load(course.cover)
                .transform(new RoundedCorners(DensityUtils.dp2px(mContext,20)))  // 16dp 圆角
                .into(holder.cover);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick(position, course);
            }
        });
        holder.collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                course.isCollect = !course.isCollect;
                listener.onCollectClick(position, course);
                notifyItemChanged(position);
            }
        });
        holder.progress.setProgress(course.progress);
        if (course.lastDate!=null){
            holder.date.setText(DateUtil.formatDate(course.lastDate));
            holder.recent_study_date.setVisibility(View.VISIBLE);
        }else {
            holder.recent_study_date.setVisibility(View.GONE);
        }

        if (course.isCollect) {
            holder.collect.setImageResource(R.mipmap.icon_collect);
        } else {
            holder.collect.setImageResource(R.mipmap.icon_collect_normal);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_course, parent, false));
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView cover;
        public TextView title;
        public CircularProgressView progress;
        public TextView date;
        public TextView recent_study_date;
        public ImageView collect;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cover = itemView.findViewById(R.id.cover);
            title = itemView.findViewById(R.id.title);
            progress = itemView.findViewById(R.id.progress);
            date = itemView.findViewById(R.id.date);
            recent_study_date = itemView.findViewById(R.id.recent_study_date);
            collect = itemView.findViewById(R.id.collect);
        }
    }
}
